﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WFL.Wrapper.Highcharts
{
    public class HighchartsObject : JsonObject
    {
        public ChartObject Chart
        {
            get { return (ChartObject)this["chart"]; }
            set { this["chart"] = value; }
        }

        public TitleObject Title
        {
            get { return (TitleObject)this["title"]; }
            set { this["title"] = value; }
        }

        public SubtitleObject Subtitle
        {
            get { return (SubtitleObject)this["subtitle"]; }
            set { this["subtitle"] = value; }
        }

        public TooltipObject Tooltip
        {
            get { return (TooltipObject)this["tooltip"]; }
            set { this["tooltip"] = value; }
        }

        public PlotOptionsObject PlotOptions
        {
            get { return (PlotOptionsObject)this["plotOptions"]; }
            set { this["plotOptions"] = value; }
        }

        public LegendObject Legend
        {
            get { return (LegendObject)this["legend"]; }
            set { this["legend"] = value; }
        }

        public object[] Series
        {
            get { return (object[])this["series"]; }
            set { this["series"] = value; }
        }

        public CreditsObject Credits
        {
            get { return (CreditsObject)this["credits"]; }
            set { this["credits"] = value; }
        }

        public XAxisObject[] XAxis
        {
            get { return (XAxisObject[])this["xAxis"]; }
            set { this["xAxis"] = value; }
        }

        public YAxisObject[] YAxis
        {
            get { return (YAxisObject[])this["yAxis"]; }
            set { this["yAxis"] = value; }
        }

        public virtual void Render(string container, TextWriter writer)
        {
            writer.Write("$(function(){$('");
            writer.Write(container);
            writer.Write("').highcharts(");
            this.Write(writer);
            writer.Write(");});");
        }

        public virtual string Render(string container)
        {
            StringBuilder script = new StringBuilder();
            StringWriter writer = new StringWriter(script);
            try
            {
                Render(container, writer);
                writer.Flush();
                return script.ToString();
            }
            finally
            {
                writer.Close();
            }
        }
    }
}
